import tkinter as tk     # 导入tkinter库
root = tk.Tk()     # 创建主窗口
root.geometry('250x380')     # 设置界面大小
root.title('Calculate')     # 设置窗口标题
frame_show = tk.Frame(width=300, height=150 ,bg='#dddddd')     # 设置Frame框架的计算器显示屏
a = tk.StringVar()     # 获取单选框Value a
a.set('0')     # 初始化Value a的值
# 设置Label的参数
show_label = tk.Label(frame_show, textvariable = a, bg = 'white', width=12, height=1, font=("Times New Roman", 20))
show_label.pack(padx = 10, pady = 10)     # 显示该Label

frame_show.pack()     # 显示Frame框架

# 初始化变量
calcul = []
ispress = False      # 判断是否按下运算符

def changenumber(num):     # 改变数值
    global ispress     # 声明全局变量，下同
    if ispress == False:
        if a.get() == '0'or a.get() == '00':
            a.set('')
            a.set(num)
        else:
            a.set(a.get()+num)
    else:
        a.set(num)
        ispress = False

def operation(sign):     # 选择运算符
    global ispress
    global calcul
    ispress = True
    calcul.append(a.get())
    if sign == '+':
        calcul.append('+')
    elif sign == '-':
        calcul.append('-')
    elif sign == '*':
        calcul.append('*')
    elif sign == '/':
        calcul.append('/')
    elif sign == '%':
        calcul.append('%')
    print(calcul)

def equal():     # 进行计算
    global calcul
    calcul.append(a.get())
    print(calcul)

    calculstr = ''.join(calcul)
    if calculstr[-1] in'+-*/%':
        calculstr = calculstr[0:-1]
    result = eval(calculstr)
    a.set(result)
    calcul.clear()

def delete():     # 删除
    if a.get() == '' or a.get() =='0' or a.get() == '00':
        a.set('0')
        return
    else:
        num = len(a.get())
        if num > 1:
            strnum = a.get()
            strnum = strnum[0:-1]
            a.set(strnum)
        else:
            a.set('0')

def clearboard():     # 清空
    global calcul
    calcul = []
    a.set('0')
    ispress = False

def signa():     # 正负颠倒
    strnum = a.get()
    if strnum[0] == '-':
        a.set(strnum[1:])
    elif strnum[0] != '-' and strnum != '0':
        a.set('-'+strnum)



# 配置按键符
frame_board = tk.Frame(width = 400, height = 350, bg = '#cccccc')
button_del = tk.Button(frame_board, text = '<-', width = 5, height = 1, command = delete).grid(row = 0, column = 0)
button_signa = tk.Button(frame_board,text = '±', width = 5, height = 1, command = signa).grid(row = 0, column = 2)
button_clearb = tk.Button(frame_board,text = 'CE', width = 5, height = 1, command = clearboard).grid(row = 0, column = 1)
button_mo = tk.Button(frame_board, text = '%', width = 5, height = 1, command = lambda:operation('%')).grid(row = 0, column = 3)
button_1 = tk.Button(frame_board, text = '1', width = 5, height = 2, command = lambda:changenumber('1')).grid(row = 1, column = 0)
button_2 = tk.Button(frame_board, text = '2', width = 5, height = 2, command = lambda:changenumber('2')).grid(row = 1, column = 1)
button_3 = tk.Button(frame_board, text = '3', width = 5, height = 2, command = lambda:changenumber('3')).grid(row = 1, column = 2)
button_plus = tk.Button(frame_board, text = '+', width = 5, height = 2, command = lambda:operation('+')).grid(row = 1, column = 3)
button_4 = tk.Button(frame_board, text = '4', width = 5, height = 2, command = lambda:changenumber('4')).grid(row = 2, column = 0)
button_5 = tk.Button(frame_board, text = '5', width = 5, height = 2, command = lambda:changenumber('5')).grid(row = 2, column = 1)
button_6 = tk.Button(frame_board, text = '6', width = 5, height = 2, command = lambda:changenumber('6')).grid(row = 2, column = 2)
button_minus = tk.Button(frame_board, text = '-', width = 5, height = 2, command = lambda:operation('-')).grid(row = 2, column = 3)
button_7 = tk.Button(frame_board, text = '7', width = 5, height = 2, command = lambda:changenumber('7')).grid(row = 3, column = 0)
button_8 = tk.Button(frame_board, text = '8', width = 5, height = 2, command = lambda:changenumber('8')).grid(row = 3, column = 1)
button_9 = tk.Button(frame_board, text = '9', width = 5, height = 2, command = lambda:changenumber('9')).grid(row = 3, column = 2)
button_mult = tk.Button(frame_board, text = '*', width = 5, height = 2, command = lambda:operation('*')).grid(row = 3, column = 3)
button_00 = tk.Button(frame_board, text = '00', width = 5, height = 2, command = lambda:changenumber('00')).grid(row = 4, column = 0)
button_0 = tk.Button(frame_board, text = '0', width = 5, height = 2, command = lambda:changenumber('0')).grid(row = 4, column = 1)
button_dot = tk.Button(frame_board, text = '.', width = 5, height = 2, command = lambda:changenumber('.')).grid(row = 4, column = 2)
button_equal = tk.Button(frame_board, text = '=', width = 5, height = 2, command = lambda:equal()).grid(row = 4, column = 3)
frame_board.pack(padx = 10,pady = 10)



root.mainloop()     # 使界面永久显示

